package io.renren.modules.message.service.impl;

import io.renren.modules.message.dao.MessageDao;
import io.renren.modules.message.entity.MessageEntity;
import io.renren.modules.message.service.MessageService;
import io.renren.modules.push.PushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;



@Service("messageService")
public class MessageServiceImpl implements MessageService {
	@Autowired
	private MessageDao messageDao;

	@Autowired
	private PushService pushService;
	
	@Override
	public MessageEntity queryObject(Integer id){
		return messageDao.queryObject(id);
	}
	
	@Override
	public List<MessageEntity> queryList(Map<String, Object> map){
		return messageDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return messageDao.queryTotal(map);
	}
	
	@Override
	public void save(MessageEntity message){
		messageDao.save(message);
        String type = message.getType();
		if ("0".equals(type)|| "1".equals(type)) {
            // 发送个推消息
            pushService.pushTransmissionMessageToSingle(message.getUsername(), message.getType(), String.valueOf(message.getId()));
        }
        if ("2".equals(type)) {
            pushService.pushTransmissionMessageToApp(message.getType(), String.valueOf(message.getId()));
        }
	}
	
	@Override
	public void update(MessageEntity message){
		messageDao.update(message);
	}
	
	@Override
	public void delete(Integer id){
        MessageEntity message = messageDao.queryObject(id);
        String type = message.getType();
        if ("0".equals(type)|| "1".equals(type)) {
            // 发送个推消息
            pushService.pushTransmissionMessageToSingle(message.getUsername(), message.getType(), String.valueOf(message.getId()));
        }
        if ("2".equals(type)) {
            pushService.pushTransmissionMessageToApp(message.getType(), String.valueOf(message.getId()));
        }
        messageDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
        for (Integer id : ids) {
            MessageEntity message = messageDao.queryObject(id);
            String type = message.getType();
            if ("0".equals(type)|| "1".equals(type)) {
                // 发送个推消息
                pushService.pushTransmissionMessageToSingle(message.getUsername(), message.getType(), String.valueOf(message.getId()));
            }
            if ("2".equals(type)) {
                pushService.pushTransmissionMessageToApp(message.getType(), String.valueOf(message.getId()));
            }
        }
		messageDao.deleteBatch(ids);
	}
	
}
